package com.cdut.drivingschool.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cdut.drivingschool.R;
/**
 * 加载控件
 * @author Administrator
 *
 */
public class LoadingView extends FrameLayout{
	private LinearLayout mLoadingLayout;
	private TextView mLoadingText;
	private TextView mLoadingFailText;
	private Context context;
	/**
	 * 加载时显示是的文字
	 */
	private String loadingText;
	public static interface RetryListener{
		void onRetry();
	}
	private RetryListener listener;
	public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initView(context);
	}

	public LoadingView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}

	public LoadingView(Context context) {
		super(context);
		initView(context);
	}
	public void initView(Context context){
		this.context = context;
		LayoutInflater.from(context).inflate(R.layout.layout_loading_view, this, true);
		mLoadingLayout = (LinearLayout) this.findViewById(R.id.loading_layout);
		mLoadingText = (TextView) this.findViewById(R.id.loading_text);
		mLoadingFailText = (TextView) this.findViewById(R.id.loading_fail_text);
		mLoadingFailText.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(listener != null){
					showLoading(loadingText);
					listener.onRetry();
				}
			}
		});
	}
	/**
	 * 设置加载文字
	 * @param text
	 */
	public void setLoadingText(String text){
		loadingText = text;
		mLoadingText.setText(text);
	}
	/**
	 * 设置加载失败文字
	 * @param text
	 */
	public void setLoadingFailText(String text){
		mLoadingFailText.setText(text);
	}
	/***
	 * 显示加载中
	 * @param text
	 */
	public void showLoading(String text){
		setLoadingText(text);
		mLoadingFailText.setVisibility(View.INVISIBLE);
		mLoadingLayout.setVisibility(View.VISIBLE);
	}
	/***
	 * 显示加载失败
	 * @param text
	 */
	public void showLoadingFail(String text){
		setLoadingFailText(text);
		mLoadingFailText.setVisibility(View.VISIBLE);
		mLoadingLayout.setVisibility(View.INVISIBLE);
	}
	/**
	 * 停止加载
	 */
	public void stopLoading(){
		if(this.getVisibility() == View.VISIBLE){
			this.startAnimation(AnimationUtils.loadAnimation(context, R.anim.fade_out));
			this.setVisibility(View.GONE);
		}
		
	}
	/**
	 * 重试监听
	 */
	public void setOnRetryListener(RetryListener listener){
		this.listener = listener;
	}
}
